<?php
/**
 * curl.libaray.php
 * @Copyright lemonCarlJ All rights reserved.
 * @License http://www.56code.com/License.txt
 * @Version 1.0.0
 * @Email rpgshifa@aliyun.com
 */
namespace SF\libaray;

class curl
{
    /**
     * 连接端口
     * @var int
     */
    public $port = null;
    
    /**
     * 设置超时
     * @var int
     */
    public $timeout = 0;
    
    /**
     * User Agent 信息
     * @var string|null
     */
    public $user_agent = null;
    
    /**
     * 是否显示header
     * @var boolean
     */
    public $header = false;
    
    /**
     * 返回Header信息
     * @var array
     */
    public $header_info = null;
    
    /**
     * 发送header头值
     * @var string|null
     */
    public $http_header = null;
    
    /**
     * 发送来路
     * @var string|null
     */
    public $referer = null;
    
    /**
     * 验证用户名
     * @var string|null
     */
    public $username = null;
    
    /**
     * 验证密码
     * @var string|null
     */
    public $password = null;
    
    /**
     * 使用代理
     * @var string
     */
    public $proxy = null;
    
    /**
     * 代理端口
     * @var string
     */
    public $proxy_port = null;
    
    /**
     * 代理类型
     * @var string
     */
    public $proxy_type = null;
    
    /**
     * 代理鉴权模式
     * @var string
     */
    public $proxy_auth = null;
    
    /**
     * 代理用户名
     * @var string
     */
    public $proxy_username = null;
    
    /**
     * 代理密码
     * @var string
     */
    public $proxy_password = null;
    
    /**
     * 对证书来源的检查
     * @var boolean
     */
    public $ssl_verifypeer = false;
    
    /**
     * 对证书进行检查
     * @var int
     */
    public $ssl_verifyhost = 0;
    
    /**
     * 证书文件
     * @var string
     */
    public $ssl_cert = null;
    
    /**
     * 证书文件类型
     * @var string
     */
    public $ssl_cert_type = null;
    
    /**
     * 证书密码
     * @var string
     */
    public $ssl_cert_passwd = null;
    
    /**
     * SSL私钥文件
     * @var string
     */
    public $ssl_key = null;
    
    /**
     * SSL私钥文件类型
     * @var string
     */
    public $ssl_key_type = null;
    
    /**
     * SSL私钥密码
     * @var string
     */
    public $ssl_key_passwd = null;
    
    /**
     * 证书验证文件
     * @var string
     */
    public $ca_info = null;
    
    /**
     * 多个验证证书文件存放目录
     * @var string
     */
    public $ca_path = null;
    
    /**
     * 跟踪定位
     * @var boolean
     */
    public $follow_location = false;
    
    /**
     * 跟踪深度
     * @var int
     */
    public $max_redirs = 0;
    
    /**
     * 发送时候的Cookie
     * @var string
     */
    public $cookie = null;
    
    /**
     * 创建一个CURL
     * @return resource
     */
    protected function create_curl()
    {
        //初始化CURL
        $curl = curl_init();
        
        //设置连接端口
        if( $port = $this->port )
        {
            curl_setopt( $curl, CURLOPT_PORT, $port );
        }
        
        //设置超时
        curl_setopt( $curl, CURLOPT_TIMEOUT, $this->timeout );
        
        //定向跟踪
        if( $this->follow_location )
        {
            curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1 );
            
            //跟踪深度
            if( $max_redirs = $this->max_redirs )
            {
                curl_setopt( $curl, CURLOPT_MAXREDIRS, $max_redirs );
            }
        }
        
        //发送Cookie
        if( $cookie = $this->cookie )
        {
            curl_setopt( $curl, CURLOPT_COOKIE, $cookie );
        }
        
        //不直接输出数据
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
        
        //自定义发送Header
        if( $header = $this->http_header )
        {
            curl_setopt( $curl, CURLOPT_HTTPHEADER, $header );
        }
        
        //接收返回Header
        if( $this->header )
        {
            curl_setopt( $curl, CURLOPT_HEADER, true);
            curl_setopt( $curl, CURLINFO_HEADER_OUT, true );
            $this->header_info = curl_getinfo( $curl, CURLINFO_HEADER_OUT );
        }else{
            $this->header_info = null;
        }
        
        //自定义Referer
        if( $referer = $this->referer )
        {
            curl_setopt( $curl, CURLOPT_REFERER, $referer );
        }
        
        //使用代理
        if( $proxy = $this->proxy )
        {
            curl_setopt( $curl, CURLOPT_PROXY, $proxy );
            
            //设定代理端口
            if( $proxy_port = $this->proxy_port )
            {
                curl_setopt( $curl, CURLOPT_PROXYPORT, $proxy_port );
            }
            
            //设定代理类型
            if( $proxy_type = $this->proxy_type )
            {
                curl_setopt( $curl, CURLOPT_PROXYTYPE, $proxy_type );
            }
            
            //账户登陆
            if( $proxy_username = $this->proxy_username )
            {
                $proxy_password = $this->proxy_password;
                curl_setopt( $curl, CURLOPT_PROXYUSERPWD, $proxy_username . ':' . $proxy_password );
            }
        }
        
        //访问验证
        if( $username = $this->username )
        {
            $password = $this->password;
            curl_setopt( $curl, CURLOPT_USERPWD, $username . ':' . $password );
        }
        
        //证书检查
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, $this->ssl_verifyhost );
        
        return $curl;
    }
    
    /**
     * GET发送数据
     * @param string $url 发送URL
     * @return mixed 返回或者结果或者操作失败
     */
    public function get( $url )
    {
        //初始化CURL
        $curl = $this->create_curl();
        
        //设置访问URL
        curl_setopt( $curl, CURLOPT_URL, $url );
        
        //执行CURL
        $result = curl_exec( $curl );
        
        //关闭CURL
        curl_close( $curl );
        
        return $result;
    }
    
    /**
     * POST发送数据
     * @param string $url 发送URL
     * @param mixed $data 发送数据
     * @return mixed 返回或者结果或者操作失败
     */
    public function post( $url, $data = null )
    {
        //初始化CURL
        $curl = $this->create_curl();
        
        //设置访问URL
        curl_setopt( $curl, CURLOPT_URL, $url );
        
        //POST提交
        curl_setopt( $curl, CURLOPT_POST, 1 );
        
        //提交数据
        if( $data )
        {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        
        //执行CURL
        $result = curl_exec( $curl );
        
        //关闭CURL
        curl_close( $curl );
        
        return $result;
    }
    
    /**
     * PUT发送数据
     * @param string $url 发送URL
     * @param mixed $data 发送数据
     * @return mixed 返回或者结果或者操作失败
     */
    public function put( $url, $data = null )
    {
        //初始化CURL
        $curl = $this->create_curl();
        
        //设置访问URL
        curl_setopt( $curl, CURLOPT_URL, $url );
        
        //使用PUT传输
        curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "PUT" );
        
        //提交数据
        if( $data )
        {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        
        //执行CURL
        $result = curl_exec( $curl );
        
        //关闭CURL
        curl_close( $curl );
        
        return $result;
    }
    
    /**
     * DELETE发送数据
     * @param string $url 发送URL
     * @param mixed $data 发送数据
     * @return mixed 返回或者结果或者操作失败
     */
    public function delete( $url, $data = null )
    {
        //初始化CURL
        $curl = $this->create_curl();
        
        //设置访问URL
        curl_setopt( $curl, CURLOPT_URL, $url );
        
        //使用PUT传输
        curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "DELETE" );
        
        //提交数据
        if( $data )
        {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        
        //执行CURL
        $result = curl_exec( $curl );
        
        //关闭CURL
        curl_close( $curl );
        
        return $result;
    }
    
    /**
     * POST发送数据[ SSL认证 ]
     * @param string $url 发送URL
     * @param mixed $data 发送数据
     * @return mixed 返回或者结果或者操作失败
     */
    public function post_ssl( $url, $data = null )
    {
        //初始化CURL
        $curl = $this->create_curl();
        
        //设置访问URL
        curl_setopt( $curl, CURLOPT_URL, $url );
        
        //POST提交
        curl_setopt( $curl, CURLOPT_POST, 1 );
        
        //证书操作
        if( $ssl_cert = $this->ssl_cert )
        {
            if( file_exists( $ssl_cert ) )
            {
                curl_setopt( $curl, CURLOPT_SSLCERT, $ssl_cert );
                //设置证书类型
                if( $ssl_cert_type = $this->ssl_cert_type )
                {
                    curl_setopt( $curl, CURLOPT_SSLCERTTYPE, $ssl_cert_type );
                }
                //设置证书密码
                if( $ssl_cert_passwd = $this->ssl_cert_passwd )
                {
                    curl_setopt( $curl, CURLOPT_SSLCERTPASSWD, $ssl_cert_passwd );
                }
            }else{
                return false;
            }
        }
        
        //私钥操作
        if( $ssl_key = $this->ssl_key )
        {
            if( file_exists( $ssl_key ) )
            {
                curl_setopt( $curl, CURLOPT_SSLKEY, $ssl_key );
                //设置私钥类型
                if( $ssl_key_type = $this->ssl_key_type )
                {
                    curl_setopt( $curl, CURLOPT_SSLKEYTYPE, $ssl_key_type );
                }
                //设置私钥密码
                if( $ssl_key_passwd = $this->ssl_key_passwd )
                {
                    curl_setopt( $curl, CURLOPT_SSLKEYPASSWD, $ssl_key_passwd );
                }
            }else{
                return false;
            }
        }
        
        //CA证书
        if( $ca_path = $this->ca_path )
        {
            if( file_exists( $ca_path ) )
            {
                curl_setopt( $curl, CURLOPT_CAPATH, $ca_path );
            }else{
                return false;
            }
        }else{
            if( $ca_info = $this->ca_info )
            {
                if( file_exists( $ca_info ) )
                {
                    curl_setopt( $curl, CURLOPT_CAINFO, $ca_info );
                }else{
                    return false;
                }
            }
        }
        
        //提交数据
        if( $data )
        {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        
        //执行CURL
        $result = curl_exec( $curl );
        
        //关闭CURL
        curl_close( $curl );
        
        return $result;
    }
}